﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace _013_WCFClientDuplex
{
    class MyCallback : _013_WCFServerDuplex.IServerCallback
    {
        public void ShowResult(int[] rq)
        {
            Console.WriteLine("Response:");
            foreach (var a in rq) Console.WriteLine("{0}", a);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter array size:");
            int size = Int32.Parse(Console.ReadLine());
            if (size > 0)
            {
                int[] array = new int[size];
                for (int i = 0; i < size; i++)
                {
                    Console.Write("Enter element {0}:", i);
                    array[i] = Int32.Parse(Console.ReadLine());
                }


                MyCallback callBack = new MyCallback();
                InstanceContext instanceContext = new InstanceContext(callBack);
                ChannelFactory<_013_WCFServerDuplex.IServerInterface> factory = new DuplexChannelFactory<_013_WCFServerDuplex.IServerInterface> 
                    (instanceContext, new System.ServiceModel.NetTcpBinding());


                try
                {
                    Console.Write("Connecting to server ...");
                    _013_WCFServerDuplex.IServerInterface srv = factory.CreateChannel(new EndpointAddress("net.tcp://localhost:8080/MyServer"));
                    Console.Write("Sending request to server ...");

                    srv.ProcessRequest(array);

                }
                finally
                {
                    factory.Close();
                }

                Console.WriteLine("Done");
                Console.ReadLine();

            }
        }
    }
}
